<?php
/**
 * 权限菜单模型
 * @author Administrator
 *
 */
namespace app\home\model;
use think\Model;
use think\Db;

class Authgroup extends Model {
	protected $table = 'hs_auth_group';
	protected $resultSetType = 'collection';
    protected $_validate = array(
        array('rules','require','[权限结点] 不能为空！',2),
        array('title','require','[角色] 不能为空！',1),
    );
	protected $pk = 'id';
	protected $field=array(
			"pid",
			"name",
			"status",
			"rules",
			"deleted",
			"sort"
	);
	/**
	 * 获取带条件的菜单权限结点
	 * @param unknown $map
	 * @return unknown
	 */
	public function lst($map=array()) {
		$map["deleted"]='0';
		$map["pid"]=array('exp','is not null');
		$list= $this->where($map)->order("sort asc")->field('title,id,sort')->select()->toArray();
		return($list);
	}
	
	/**
	 * 获取单条数据
	 * @param unknown $map
	 * @return Ambigous <\think\static, NULL>
	 */
	public function getone($map){
		return $this->get($map)->toArray();
	}

	
	public function FixNode($arr,$fid=0)
	{
		$nodes = array();
		foreach($arr as $row)
		{
			unset($row["haschild"]);
			unset($row["condition"]);
			unset($row["remark"]);
			unset($row["weigh"]);
			unset($row["status"]);
			
			if($row["pid"] == $fid)
			{
				$row["children"]= $this->FixNode($arr,$row['id']);
				$nodes[] =$row;
			}
		}
		return $nodes;
	}
	
	
	/**
	 * 获取单条数据
	 * @param unknown $map
	 * @return Ambigous <\think\static, NULL>
	 */
	public function savedata($data,$map){
		print_r($map);
		print_r($data);
		 echo $this->where($map)->update($data);
		 return;
	}
	
	/**
	 * 获取带所有子id的节点
	 * @return unknown
	 */
	public function lst_sub(){
		$data=$this->lst();
		
		foreach($data as $key=>$val){
			$subid=getSonNode($data,$val["deptid"],$SonNode = array());;
			$data[$key]["subid"]=$subid;
		}
		return $data;
	}

	/**
	 * 方法名：editRule
	 * 方法说明：修改权限
	 * @param requset
	 * create_name:Beyonce
	 * create_time:2018/6/20  9:41
	 * remake:
	 */
	
	public function editRule($request)
	{
 		Db::startTrans();
        try{
            $id = $request['id'];
            $map_ag['id'] = array('neq',$id);
            $map_ag['title'] = $request['row']['title'];
            $map_ag['deleted'] = 0;
            $status= Db::table('rrs_auth_group')->where($map_ag)->find();

            if(!empty($status)) {Db::rollback();return ['code'=>1,'info'=>'权限名称已重复','msg'=>'权限名称已重复','data'=>''];}
            /*存在对比规则 不相等修改规则*/

			if(empty( $request['row']['rules'])){
                {Db::rollback();return ['code'=>1,'info'=>'请选择权限','msg'=>'请选择权限','data'=>''];}
            }

				$data['rules'] = $request['row']['rules'];
				$data['title'] = $request['row']['title'];
				$data['status'] = 1;
				$data['pid'] = 1;
				$data['sort'] = 0;
				$data['id'] = $request['id'];

				$status = $this->allowField(true)->saveall([$data]);
				if(!$status) {Db::rollback();return ['code'=>1,'info'=>'操作失败2','msg'=>'操作失败2','data'=>''];}


				$status = Db::table('rrs_auth_group_access')->where('group_id',$id)->select();
	            if(count($status)>0){
	                $status = Db::table('rrs_auth_group_access')->where('group_id',$id)->delete();
	                if(!$status) {Db::rollback();return ['code'=>1,'info'=>'操作失败4','msg'=>'操作失败4','data'=>''];}
	            }
	            if(empty($request['row']['userid'])){
	            	Db::commit();return ['code'=>0,'info'=>'操作成功','msg'=>'操作成功','data'=>''];
	            }
	            /*获取全部选中的ID 重写写入*/
	            $request = explode(',',$request['row']['userid']);
	            foreach ($request as $key => $value) {
	                $new[$key]['group_id'] = $id;
	                $new[$key]['uid'] = $value;

	            }
	            //$AuthGroupAccess = new \app\home\model\AuthGroupAccess();
            	$status = model('AuthGroupAccess')->allowField(true)->saveAll($new);
            	if($status){
            		Db::commit();
                	return ['code'=>0,'info'=>'操作成功','msg'=>'操作成功','data'=>''];
           		 }
           		 	Db::rollback();
            		return ['code'=>1,'info'=>'操作失败5','msg'=>'操作失败5','data'=>''];

            
            } catch (\Exception $e) {
                        Db::rollback();
                        return ["code"=>1,"info"=>$e->getMessage(),"msg"=>$e->getMessage()];
            }
	}

    public function getStatusAttr($value)
    {
        return $value == 0 ? false : true;
    }
}
